define(function(require, exports){
	var $ = require("jquery");
	var pubjs = require('pubjs');
	var view = require('@base/view');
	var util = require('util');

	var List = view.container.extend({
		init:function(config){
			config = pubjs.conf(config, {
				'class': 'P-product',
				'urlDelete': 'materialGroup/remove',
				'view_model': {
					'view': {
						'addButton': pubjs.checkRight('materialGroup.add')
					}
				}
			});
			this.Super('init', arguments);
		},
		afterBuild:function(){
			var self = this;
			var c = self.getConfig();

			self.append($([
				'<div class="M-formHead">',
					'<span class="uk-button-primary" pub-name="add" pub-mod="@base/common/input.button" pub-auth="materialGroup.add"/>',
					'<span class="fr" pub-name="date" pub-mod="@base/common/date.dateRangeNew"/>',
				'</div>',
				'<div class="cl" pub-name="highGrid" pub-mod="materialGroup/grid/list.base"/>'
			].join('')));
			self.buildFromTemplate({
				'highGrid': {
					cols: [
						{name:'_id',text:"ID", sort: true, width: 60, align: 'center'},
						{name:'Name', text: LANG("名称"), width: 200, align: 'center'},
						{name:'StrategyInfo', text: LANG("创意包落地页"), width: 300, render: 'renderStrategy', align: 'center'},
						{name:'UpdateTime', text: LANG("更新时间"), render: 'renderTime', align: 'center'}
					]
				},
				'date': {
					'pos': 'bR'
				},
				add: {
					value: LANG('添加')
					,'class': 'btn btn-primary'
					,width: null
				}
			});

			self.createAsync('editPop', 'materialGroup/action/edit.popwin', {
			});
		},
		onInputClick: function(ev) {
			switch (ev.name) {
				case 'add':
					this.showEdit();
					break;
				default :
					return false;
			}

			return false;
		},
		showEdit: function (id) {
			var mod = this.$.editPop;
			if(mod)
			{
				mod.reset().show();
				if(id)
				{
					mod.load(id);
					mod.setTitle(LANG('编辑素材组'))
				}
				else
				{
					mod.setTitle(LANG('添加素材组'))
				}
			}
		},
		onMaterialGroupOk: function () {
			this.reload();
		},
		// 操作列点击事件
		onOperateMenuShow: function( ev ) {
			var param = ev.param;
			var result = [];
			if (pubjs.checkRight('materialGroup.update')) {
				result.push({'id': 'edit', 'name': LANG('编辑')});
			}

			if (pubjs.checkRight('materialGroup.remove')) {
				if(!param.IsDeleted)
				{
					result.push({'id': 'delete', 'name': LANG('删除')});
				}
			}

			ev.returnValue = result;
			return false;
		},
		// 操作列选项点击事件
		onOperateMenuSelect: function( ev ) {
			var key = ev.param[0]['key'];
			var id = ev.param[1]['_id'];
			var c = this.getConfig();
			var self = this;
			switch(key){
				case 'edit':
					this.showEdit(id);
				break;
				case 'delete':
					pubjs.confirm(LANG('确认删除素材组 %1 吗', id), function(state) {
						if(state)
						{
							pubjs.loading.show();
							pubjs.mc.send(c.urlDelete, {'_id': [+id]}, function(err) {
								pubjs.loading.hide();
								if(err)
								{
									return pubjs.alert(err.message);
								}

								pubjs.alert(LANG('删除成功'));
								self.reload();
							});
						}
					});
				break;
			}
		},
		// 批量操作-下拉菜单界面
		onBatchShow: function(ev) {
			var list = [];
			var batch = [
				{id: 'delete', name: LANG('归档'), right: 'remove'},
			];

			// 检查权限
			util.each(batch, function(item){
				if (pubjs.checkRight('materialGroup.' + (item.right || item.id)))
				{
					list.push(item);
				}
			});

			ev.returnValue = list;
		},
		// 批量操作-响应菜单操作
		onBatchSelect: function(ev) {
			var self = this;
			var data = ev.param[0];
			var ids = ev.param[1];

			if(!ids || !ids.length)
			{
				pubjs.alert(LANG('请先选择素材组'));
				return false;
			}

			var c = this.getConfig();
			switch(data.key)
			{
				case 'delete':
					pubjs.confirm(LANG('确认删除以下素材组 %1 吗', ids), function (state) {
						if (state)
						{
							pubjs.loading.show();
							pubjs.mc.send(c.urlDelete, {'_id': ids}, function(err, data) {
								pubjs.loading.hide();
								if(err)
								{
									return pubjs.alert(err.message);
								}

								pubjs.alert(LANG('删除成功'));
								self.reload();
							});
						}
					});
					break;
			}
		},
		reload: function( param ) {
			this.$.highGrid.reload();
		},
		onSidebarFilter: function(ev){
			var self = this
				,param = {};

			util.each(ev.param, function(item, idx){
				var type = idx;
				var id = item.id;
				switch (type) {
				}
			});

			self.$.highGrid.reload(param);
		}
	});

	exports.base = List;
});